<?php
// +---------------------------------------------------------------------------------
// | ECODE [ 随心而行 勿忘始终]
// +---------------------------------------------------------------------------------
// | Copyright (c) 2017 ECODE All rights reserved.
// +---------------------------------------------------------------------------------
// | Author: 鬼谷子 <majianwei168@outlook.com>
// +---------------------------------------------------------------------------------
namespace app\admin\model;
use think\Model;
class Article extends Model
{
    protected $autoWriteTimestamp = true;
    protected $type = ['create_time'  =>  'timestamp', 'update_time' => 'timestamp'];
    /**
     * 获取一组文章数据
     * @param  array   $map   检索条件
     * @param  string  $order 排序规则
     * @param  integer $limit 每页显示的数量
     * @return array         
     */
    protected function lists($map='', $order='ID DESC', $limit=10)
    {
        $obj = $this->where(array('is_delete' => 1))->where($map)->order($order)->paginate($limit);
        $list= $obj->toArray();
        $page= $obj->render();
        $data ['list'] = $list ['data'];
        $data ['page'] = $page;
        return $data;
    }
    /**
     * 获取一条文章数据
     * @param  int $id 文章ID
     * @return array   一条文章数据
     */
    protected function one($id)
    {
        $data = $this::get($id);
        if((!isset($data['is_delete']) || $data['is_delete'] == -1) || $data == null) {
            return -1; //数据已删除;
        } else {
            return $data->toArray();
        }
    }
    /**
     * 文章数据保存或更新
     * @param  array $data  文章数据
     * @param  int $id   
     * @return bool|int       成功或失败或是增加的ID
     */
    protected function updates($data, $id=null)
    {
        $where = $id ? array('id' => $id) : null;
       	$res = $this->validate(true)->save($data, $where);
        if( $res === false) {
            return $this->getError();
        } else {
            return $id ? 1 : (int)$this->id; //编辑返回 true  添加返增加ID
        }
    }
    /**
     * 软删除 删除一条文章数据
     * @param  int $id      文章ID
     * @return bool|int     (错误码) ||  (ture 删除成功)
     */
    protected function del($id)
    {
        $isDel = $this::where('id',$id)->value('is_delete');
        if($isDel == -1 || $isDel == null) {
            return -10006; 
        }
        $res = $this->save(array('is_delete'=> -1), array('id' => $id));
        if( $res === false ) {
            return -10007; 
        }
        return 1;
    }
    /**
     * 软删除  删除多条文章数据
     * @param  array $id 文章ID
     * @return bool      成功||失败
     */
    protected function dels($ids)
    {
        $delSuccess = true;
        $error      = array();
        foreach ($ids as $id) {
            $res = $this->data(array('is_delete'=> -1, 'id' =>$id),true)->isUpdate(true)->save();
            if( $res < 1 ) {
                $delSuccess = false;
                $error [$id]['id']    = $id;
                $error [$id]['code']  = -10008;
            }
        }
        if($delSuccess) {
            return 1;
        } else {
            return $error;
        }
    }

    protected function getStatusAttr($value)
    {
        $status = [-1=>'禁用',1=>'正常'];
        return $status[$value];
    }
    protected function getCidAttr($value)
    {
        $cidName = [0=>'未分类'];
        return $cidName[$value];
    }
}